package engine.math
{
	import engine.core.*;
	
	public class RxVector4
	{
		public var x:Number;
		public var y:Number;
		public var z:Number;
		public var w:Number;
		private static var s_pool:RxObjectPool = null;
		
		public function RxVector4(ix:Number = 0, iy:Number = 0, iz:Number = 0, iw:Number = 0)
		{
			this.x = ix;
			this.y = iy;
			this.z = iz;
			this.w = iw;
		}
		
		public static function CreatePool(initSize:uint, growSize:uint) : void
		{
			var allocFunc:* = function () : Object
			{
				return new RxVector4();
			};
			s_pool = new RxObjectPool(allocFunc, null, initSize, growSize, RxVector4);
		}
		
		public function Copy(vector4:RxVector4) : void
		{
			this.x = vector4.x;
			this.y = vector4.y;
			this.z = vector4.z;
			this.w = vector4.w;
		}
		
		public function Set(ix:Number = 0, iy:Number = 0, iz:Number = 0, iw:Number = 0) : void
		{
			this.x = ix;
			this.y = iy;
			this.z = iz;
			this.w = iw;
		}
		
		public function CopyFromString(str:String) : void
		{
			var arr:Array = null;
			if (str != "")
			{
				arr = str.match(RxUtilities.s_regExpFloat4);
				this.x = Number(arr[1]);
				this.y = Number(arr[2]);
				this.z = Number(arr[3]);
				this.w = Number(arr[4]);
			}
		}
		
		public static function Distance3(f_vector:RxVector4, s_vector:RxVector4) : Number
		{
			return Math.sqrt(DistanceSquared3(f_vector, s_vector));
		}
		
		public static function DistanceSquared3(f_vector:RxVector4, s_vector:RxVector4) : Number
		{
			var dis_x:* = s_vector.x - f_vector.x;
			var dis_y:* = s_vector.y - f_vector.y;
			var dis_z:* = s_vector.z - f_vector.z;
			return dis_x * dis_x + dis_y * dis_y + dis_z * dis_z;
		}
		
		public static function PoolAlloc() : RxVector4
		{
			return RxVector4(s_pool.Alloc());
		}
		
		public static function PoolFree(vector:RxVector4) : void
		{
			s_pool.Free(vector);
		}
		
		public static function CrossProduct3(vector1:RxVector4, vector2:RxVector4, vector3:RxVector4) : void
		{
			var x:* = vector2.y * vector3.z - vector2.z * vector3.y;
			var y:* = vector2.z * vector3.x - vector2.x * vector3.z;
			var z:* = vector2.x * vector3.y - vector2.y * vector3.x;
			vector1.x = x;
			vector1.y = y;
			vector1.z = z;
			vector1.w = vector2.w;
		}
		
		public function Magnitude3() : Number
		{
			return Math.sqrt(this.MagnitudeSquared3());
		}
		
		public function MagnitudeSquared3() : Number
		{
			return this.x * this.x + this.y * this.y + this.z * this.z;
		}
		
		public static function DivScalar(vector1:RxVector4, vector2:RxVector4, mag:Number) : void
		{
			mag = 1 / mag;
			vector1.x = vector2.x * mag;
			vector1.y = vector2.y * mag;
			vector1.z = vector2.z * mag;
			vector1.w = vector2.w * mag;
		}
		
		public static function MulMatrix4x4(param1:RxVector4, param2:RxVector4, param3:RxMatrix4x4) : void
		{
			var _loc_4:* = param2.x * param3.m00 + param2.y * param3.m10 + param2.z * param3.m20 + param2.w * param3.m30;
			var _loc_5:* = param2.x * param3.m01 + param2.y * param3.m11 + param2.z * param3.m21 + param2.w * param3.m31;
			var _loc_6:* = param2.x * param3.m02 + param2.y * param3.m12 + param2.z * param3.m22 + param2.w * param3.m32;
			var _loc_7:* = param2.x * param3.m03 + param2.y * param3.m13 + param2.z * param3.m23 + param2.w * param3.m33;
			param1.x = _loc_4;
			param1.y = _loc_5;
			param1.z = _loc_6;
			param1.w = _loc_7;
		}
		
		public static function MulScalar(param1:RxVector4, param2:RxVector4, param3:Number) : void
		{
			param1.x = param2.x * param3;
			param1.y = param2.y * param3;
			param1.z = param2.z * param3;
			param1.w = param2.w * param3;
		}
		
		public static function Add(param1:RxVector4, param2:RxVector4, param3:RxVector4) : void
		{
			param1.x = param2.x + param3.x;
			param1.y = param2.y + param3.y;
			param1.z = param2.z + param3.z;
			param1.w = param2.w + param3.w;
		}
		
		public static function MulMatrix4x3(param1:RxVector4, param2:RxVector4, param3:RxMatrix4x4) : void
		{
			var _loc_4:* = param2.x * param3.m00 + param2.y * param3.m10 + param2.z * param3.m20 + param2.w * param3.m30;
			var _loc_5:* = param2.x * param3.m01 + param2.y * param3.m11 + param2.z * param3.m21 + param2.w * param3.m31;
			var _loc_6:* = param2.x * param3.m02 + param2.y * param3.m12 + param2.z * param3.m22 + param2.w * param3.m32;
			param1.x = _loc_4;
			param1.y = _loc_5;
			param1.z = _loc_6;
			param1.w = param2.w;
		}
		
		public function UnpackBGRA(param1:uint) : void
		{
			this.x = Number(param1 >> 16 & 255) * (1 / 255);
			this.y = Number(param1 >> 8 & 255) * (1 / 255);
			this.z = Number(param1 & 255) * (1 / 255);
			this.w = Number(param1 >> 24 & 255) * (1 / 255);
		}
		
		public static function Sub(param1:RxVector4, param2:RxVector4, param3:RxVector4) : void
		{
			param1.x = param2.x - param3.x;
			param1.y = param2.y - param3.y;
			param1.z = param2.z - param3.z;
			param1.w = param2.w - param3.w;
		}
		
		public static function DotProduct3(param1:RxVector4, param2:RxVector4) : Number
		{
			return param1.x * param2.x + param1.y * param2.y + param1.z * param2.z;
		}
	}
}